#region Using Statements
using System;
using MilwormsGameData;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
#endregion

namespace MilwormsGame
{
    class QuestNpcScreen : NpcScreen<QuestNpc>
    {
        private int licznikQ = 0;
        private int licznikA = -1;
        private List<Dialog> dQuest = new List<Dialog>();
        private List<Dialog> dAns = new List<Dialog>();
        private bool quest = false;
        private bool questCmp = false;
        private bool introduction = false;
        private bool forceDemScr = false;


        private Texture2D backgroundTexture;
        private Vector2 backgroundPosition;
        private Texture2D fadeTexture;

        protected Texture2D selectButtonTexture;
        protected Vector2 selectPosition;
        protected Vector2 selectButtonPosition;

        protected Vector2 backPosition;
        protected Texture2D backButtonTexture;
        protected Vector2 backButtonPosition;

        private Texture2D scrollTexture;
        private Vector2 scrollPosition;

        private Vector2 titlePosition;
        private Vector2 dialogueStartPosition;

        string backText = "Back";
        string selectText = "Next";

        Rectangle lowerArrow;
        Rectangle upperArrow;
        private int startIndex = 0;
     
        private const int drawMaxLines =5;
        private int endIndex = drawMaxLines;
        private string helpText="";

        private List<string> textLines;

        /// <summary>
        /// Constructs a new QuestNpcScreen object.
        /// </summary>
        /// <param name="mapEntry">The map entry for the quest NPC.</param>
        public QuestNpcScreen(MapEntry<QuestNpc> mapEntry)
            : base(mapEntry)
        {
            // assign and check the parameter
            QuestNpc questNpc = character as QuestNpc;
            if (questNpc == null)
            {
                throw new ArgumentException(
                    "QuestNpcScreen requires a MapEntry with a QuestNpc");
            }


            ////
            

            ///////

            //Sprawdza ktora misja z wykorzystaniem tej samej postaci
            if (Session.Quest.Name.Equals("Village Sharan 3/3"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(2))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(2))
                    {
                        dAns.Add(d);
                    }
                }
            }
            else if (Session.Quest.Name.Equals("Police patrol"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(2))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(2))
                    {
                        dAns.Add(d);
                    }
                }
                forceDemScr = true;
            }
            else if (Session.Quest.Name.Equals("Reinforcements"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(2))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(2))
                    {
                        dAns.Add(d);
                    }
                }
            }
            else if (Session.Quest.Name.Equals("Soviet base"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(5))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(5))
                    {
                        dAns.Add(d);
                    }
                }
            }
            else if (Session.Quest.Name.Equals("Cure"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(2))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(2))
                    {
                        dAns.Add(d);
                    }
                }
            }
            else if (Session.Quest.Name.Equals("Special Care"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(2))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(2))
                    {
                        dAns.Add(d);
                    }
                }
            }
            else if (Session.Quest.Name.Equals("Finding the secret"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(2))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(2))
                    {
                        dAns.Add(d);
                    }
                }
            }
            else if (Session.Quest.Name.Equals("DefusalKit"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(3))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(3))
                    {
                        dAns.Add(d);
                    }
                }
            }
            else if (Session.Quest.Name.Equals("GoldInformation"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(4))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(4))
                    {
                        dAns.Add(d);
                    }
                }
            }
            else if (Session.Quest.Name.Equals("Hospital"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(3))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(3))
                    {
                        dAns.Add(d);
                    }
                }
            }
            else if (Session.Quest.Name.Equals("Truth"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(4))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(4))
                    {
                        dAns.Add(d);
                    }
                }
            }
            else if (Session.Quest.Name.Equals("NuclearWarhead"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(6))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(6))
                    {
                        dAns.Add(d);
                    }
                }
            }
            else if (Session.Quest.Name.Equals("Building hospital"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(4))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(4))
                    {
                        dAns.Add(d);
                    }
                }
            }
            else if (Session.Quest.Name.Equals("Pana secrets"))
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(3))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(3))
                    {
                        dAns.Add(d);
                    }
                }
            }
            else
            {
                foreach (Dialog d in questNpc.Dialogs)
                {
                    if (d.Name.Equals("question") && d.Lp.Equals(1))
                    {
                        dQuest.Add(d);
                    }
                    else if (d.Name.Equals("answer") && d.Lp.Equals(1))
                    {
                        dAns.Add(d);
                    }
                }
            }

            // check to see if this is NPC is the current quest destination
            if ((Session.Quest != null) &&
                (Session.Quest.Stage == Quest.QuestStage.RequirementsMet) &&
                TileEngine.Map.AssetName.EndsWith(
                    Session.Quest.DestinationMapContentName) &&
                (Session.Quest.DestinationNpcContentName == mapEntry.ContentName))
            {
                //Oznacza ze osoba wypelni task
                quest = true;
                helpText = dQuest[licznikQ].Txt;
                backText = "Select 1";
                selectText = "Select 2";
            }
            else
            {
                // this NPC is not the destination, so use the npc's welcome text
                helpText = questNpc.Introduction;
                introduction = true;
            }
            textLines = Fonts.BreakTextIntoList(helpText, Fonts.DescriptionFont,
                (int)(790 * ScaledVector2.ScaleFactor));


            InputManager.GesturePositionAtThisMoment = new Vector2(90, 385);



        }










        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;

            fadeTexture =
                content.Load<Texture2D>(@"Textures\GameScreens\FadeScreen");
            backgroundTexture =
                content.Load<Texture2D>(@"Textures\GameScreens\PopupScreenNone");
            scrollTexture =
                content.Load<Texture2D>(@"Textures\GameScreens\ScrollButtons");
            selectButtonTexture = content.Load<Texture2D>(@"Textures\Buttons\rpgbtn");
            backButtonTexture = content.Load<Texture2D>(@"Textures\Buttons\rpgbtn");

            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;

            upperArrow = new Rectangle((int)scrollPosition.X - 40, (int)scrollPosition.Y - 40,
                             80, 80);
            lowerArrow = new Rectangle((int)scrollPosition.X - 40, (int)scrollPosition.Y + 100,
                             80, 80);

            backgroundPosition.X = (viewport.Width - (backgroundTexture.Width * ScaledVector2.DrawFactor)) / 2;
            backgroundPosition.Y = (viewport.Height - (backgroundTexture.Height * ScaledVector2.DrawFactor)) / 2;



            selectButtonPosition.X = viewport.Width / 2 + 130 * ScaledVector2.ScaleFactor;
            selectButtonPosition.Y = backgroundPosition.Y + 520f * ScaledVector2.ScaleFactor;
            selectPosition.X = selectButtonPosition.X - Fonts.ButtonNamesFont.MeasureString(selectText).X - 10f;
            selectPosition.Y = selectButtonPosition.Y;

            backPosition.X = viewport.Width / 2 - 140f * ScaledVector2.ScaleFactor;
            backPosition.Y = backgroundPosition.Y + 520f * ScaledVector2.ScaleFactor;
            backButtonPosition.X = backPosition.X - (backButtonTexture.Width * ScaledVector2.DrawFactor) - 10;
            backButtonPosition.Y = backPosition.Y;

            scrollPosition = backgroundPosition + ScaledVector2.GetScaledVector(820f, 200f);

    

        }









        public override void HandleInput()
        {

            if (InputManager.IsButtonClicked(new Rectangle(
                (int)selectButtonPosition.X, (int)selectButtonPosition.Y,
                selectButtonTexture.Width, selectButtonTexture.Height)))
            {
                //Button 2
                if (quest)
                {
                    if (selectText.Equals("Next") && questCmp && quest)
                    {
                        helpText = Session.Quest.CompletionMessage;
                        Session.Quest.Stage = Quest.QuestStage.Completed;
                        quest = false;
                    }
                    if (selectText.Equals("Next"))
                    {

                        ExitScreen();
                        return;
                    }
                    if (dQuest[licznikQ].Val == 2 && selectText.Equals("Select 2"))
                    {
                        licznikQ++;
                        //licznikA++;
                        helpText = dQuest[licznikQ].Txt;
                        if (licznikQ == dQuest.Count - 1)
                        {
                            questCmp = true;
                            selectText = "Next";
                            backText = "Back";
                            if (forceDemScr)
                            {
                                ScreenManager.AddScreen(new ForceDemScreen());
                            }
                            foreach (QuestRequirement<Gear> gearRequirement in
                                Session.Quest.GearRequirements)
                            {
                                Gear gear = gearRequirement.Content;
                                Session.Party.RemoveFromInventory(gear,
                                    gearRequirement.Count);
                            }
                        }
                    }
                    else if (selectText.Equals("Select 2"))
                    {
                        licznikA++;
                        helpText = dAns[licznikA].Txt;
                        selectText = "Next";
                        backText = "Back";
                    }
                }
                else if (introduction)
                {
                    ExitScreen();
                    return;
                }
            }

            if (InputManager.IsButtonClicked(new Rectangle(
                (int)backButtonPosition.X, (int)backButtonPosition.Y,
                backButtonTexture.Width, backButtonTexture.Height)))
            {
                //Button 1
                if (quest)
                {
                    if (selectText.Equals("Next") && questCmp && quest)
                    {
                        helpText = Session.Quest.CompletionMessage;
                        Session.Quest.Stage = Quest.QuestStage.Completed;
                        quest = false;
                    }
                    if (selectText.Equals("Next"))
                    {
                        ExitScreen();
                        return;
                    }
                    if (dQuest[licznikQ].Val == 1 && backText.Equals("Select 1"))
                    {
                        licznikQ++;
                        //licznikA++;
                        helpText = dQuest[licznikQ].Txt;
                        if (licznikQ == dQuest.Count - 1)
                        {
                            questCmp = true;
                            selectText = "Next";
                            backText = "Back";
                            if (forceDemScr)
                            {
                                ScreenManager.AddScreen(new ForceDemScreen());
                            }
                            foreach (QuestRequirement<Gear> gearRequirement in
                                Session.Quest.GearRequirements)
                            {
                                Gear gear = gearRequirement.Content;
                                Session.Party.RemoveFromInventory(gear,
                                    gearRequirement.Count);
                            }
                        }
                    }
                    else if (backText.Equals("Select 1"))
                    {
                        licznikA++;
                        helpText = dAns[licznikA].Txt;
                        selectText = "Next";
                        backText = "Back";
                    }
                }
                else if (introduction)
                {
                    ExitScreen();
                    return;
                }
            }
            bool upperClicked = false;
            bool lowerClicked = false;

            if (InputManager.IsButtonClicked(upperArrow))
            {
                upperClicked = true;
            }

            if (InputManager.IsButtonClicked(lowerArrow))
            {
                lowerClicked = true;
            }



            // Scroll up
            if (InputManager.IsActionTriggered(InputManager.Action.CursorUp) || upperClicked)
            {
                if (startIndex > 0)
                {
                    startIndex--;
                    endIndex--;
                }
            }
            // Scroll down
            else if (InputManager.IsActionTriggered(InputManager.Action.CursorDown) || lowerClicked)
            {
                if (startIndex < textLines.Count - drawMaxLines)
                {
                    endIndex++;
                    startIndex++;
                }
            }

            // Press Select or back
            else if (InputManager.IsActionTriggered(InputManager.Action.Back)
                || InputManager.IsButtonClicked(new Rectangle(
                     (int)backButtonPosition.X,
                     (int)backButtonPosition.Y,
                     (int)(backButtonTexture.Width * ScaledVector2.DrawFactor),
                     (int)(backButtonTexture.Height * ScaledVector2.DrawFactor)))
                || InputManager.IsButtonClicked(new Rectangle(
                     (int)selectButtonPosition.X,
                     (int)selectButtonPosition.Y,
                     (int)(selectButtonTexture.Width * ScaledVector2.DrawFactor),
                     (int)(selectButtonTexture.Height * ScaledVector2.DrawFactor))))
            {
                ExitScreen();
                return;
            }
            textLines = Fonts.BreakTextIntoList(helpText, Fonts.DescriptionFont,
               (int)(790 * ScaledVector2.ScaleFactor));
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            Vector2 textPosition = dialogueStartPosition;

            spriteBatch.Begin();






            // draw the fading screen
            spriteBatch.Draw(fadeTexture, ScaledVector2.GetScaledVector(1280, 720), null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);

            // draw popup background
            spriteBatch.Draw(backgroundTexture, backgroundPosition, null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);

            // draw scrollbar
            spriteBatch.Draw(scrollTexture, scrollPosition, null, Color.White, 0f,
                Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);

            // draw title


            // draw the dialogue

            // draw the Back button and adjoining text
            if (!String.IsNullOrEmpty(backText))
            {
                Vector2 backTextPosition = Fonts.GetCenterPositionInButton(Fonts.ButtonNamesFont, backText,
                    new Rectangle((int)backButtonPosition.X, (int)backButtonPosition.Y,
                        backButtonTexture.Width, backButtonTexture.Height));

                spriteBatch.Draw(backButtonTexture, backButtonPosition, null, Color.White, 0f,
                        Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);

                spriteBatch.DrawString(Fonts.ButtonNamesFont, backText, backTextPosition,
                    Color.White);

            }

            // draw the Select button and adjoining text
            if (!String.IsNullOrEmpty(selectText))
            {
                selectPosition.X = selectButtonPosition.X -
                    Fonts.ButtonNamesFont.MeasureString(selectText).X - 10f;
                selectPosition.Y = selectButtonPosition.Y;

                Vector2 selectTextPosition = Fonts.GetCenterPositionInButton(Fonts.ButtonNamesFont, selectText,
                        new Rectangle((int)selectButtonPosition.X, (int)selectButtonPosition.Y,
                            selectButtonTexture.Width, selectButtonTexture.Height));

                spriteBatch.Draw(selectButtonTexture, selectButtonPosition, null, Color.White, 0f,
                     Vector2.Zero, ScaledVector2.DrawFactor, SpriteEffects.None, 0f);

                spriteBatch.DrawString(Fonts.ButtonNamesFont, selectText, selectTextPosition,
                    Color.White);

            }


            if (textLines.Count <= drawMaxLines)
            {
                System.Diagnostics.Debug.WriteLine("tututu222222222222222222"+textLines.Count);
                for (int i = 0; i < textLines.Count; i++)
                {
                    spriteBatch.DrawString(Fonts.DescriptionFont, textLines[startIndex + i],
                        ScaledVector2.GetScaledVector(200, 250 + (Fonts.DescriptionFont.LineSpacing + 15) * i),
                        Color.White);
                }
            }
            else if(textLines.Count>drawMaxLines)
            {
                System.Diagnostics.Debug.WriteLine("tututut");
                for (int i = 0; i < drawMaxLines; i++)
                {
                    spriteBatch.DrawString(Fonts.DescriptionFont, textLines[startIndex + i],
                        ScaledVector2.GetScaledVector(200, 250 + (Fonts.DescriptionFont.LineSpacing + 15) * i),
                        Color.White);
                }
            }


            spriteBatch.End();
        }
    }
}
